package ym.controller.patient;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ym.pojo.MealPlan;
import ym.service.MealPlanService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 膳食计划
 */
@CrossOrigin
@RestController
@RequestMapping("/front/mealPlan")
public class FrontMealPlanCon {

    @DubboReference
    private MealPlanService mealPlanService;

    /**
     * 新增
     *
     * @param mealPlan
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(MealPlan mealPlan) {
        mealPlanService.add(mealPlan);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param mealPlan
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(MealPlan mealPlan) {
        mealPlanService.update(mealPlan);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param mealPlanId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer mealPlanId) {
        mealPlanService.delete(mealPlanId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param mealPlanId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer mealPlanId) {
        MealPlan mealPlan = mealPlanService.findById(mealPlanId);
        return BaseResult.ok(mealPlan);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<MealPlan> all = mealPlanService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    public BaseResult search(String search, int page, int size) {
        Page<MealPlan> mealPlanPage = mealPlanService.search(search, page, size);
        return BaseResult.ok(mealPlanPage);
    }

    /**
     * 根据NutritionPlanId分页查询
     *
     * @param nutritionPlanId
     * @param search
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/searchByNutritionPlanId")
    public BaseResult searchByNutritionPlanId(Integer nutritionPlanId, String search, int page, int size) {
        Page<MealPlan> mealPlanPage = mealPlanService.searchByNutritionPlanId(nutritionPlanId, search, page, size);
        return BaseResult.ok(mealPlanPage);
    }
}
